/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import shopping.Item;

/**
 *
 * @author mpower
 */
public class ShoppingDB {
    private static Connection conn;
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/shopping","root","root");
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        catch(ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static void addItems(List<Item> items) throws SQLException {
        PreparedStatement ps=conn.prepareStatement("insert into Cart values(?,?,?,?)");
        for(Item item:items) {
            ps.setString(1, item.getItemCode());
            ps.setString(2, item.getItemName());
            ps.setInt(3, item.getQty());
            ps.setDouble(4, item.getPrice());
            ps.addBatch();
        }
        ps.executeBatch();
    }
}